#!/bin/bash
# ##################################################################
# ### Service start script
# ##################################################################

# 方便查看日志，参数为 -f
PRG="$0"
P1=$1
#PRGDIR=`dirname "$PRG"`
PRGDIR=`pwd`

# ### 启动成功提示！
function success () {
    echo "#####"
    echo "####################### Start Service End! #######################"
    echo ""
    echo -e "                    \033[32;1;5m [ START SUCCESSFULLY! ]\033[0m"
    echo ""
}

# ### 启动失败提示警告！
function failed () {
    echo "#####"
    echo "####################### Start Service End! #######################"
    echo ""
    echo -e "                   \033[37;41;1;5m [ FAILED! FAILED! FAILED! ]\033[0m"
    echo -e "       \033[37;41;1;5m [ $1 ]\033[0m"
    echo ""
}

echo "####################### Start Service ############################"
echo "#####"
echo "##### Current Working Path: $PWD"

# ### Script running flag!
STATUS="OK!"

JAVA_VERSION=`java -version 2>&1 |awk 'NR==1{ gsub(/"/,""); print $3 }'`
echo "##### Java Version : $JAVA_VERSION"

# ### 判断jdk版本是否是1.8
JAVA_VERSION_P="1.8"
JAVA_PATH=""
if [[ $JAVA_VERSION =~ $JAVA_VERSION_P ]]
then
    echo "##### The JDK is OK!"
    JAVA_PATH=`which java`
else
    echo "##### The JDK is not supported!"
fi

# ### 扫描/opt和/usr目录获取JAVA_PATH
reg='jdk1.8.0_[0-9]{1,3}/bin/java'
if [ "$JAVA_PATH" = "" ]; then
    echo "##### Find JDK directory!"
    echo "##### '/opt' And '/usr' directory will be scanned."
    echo "##### Start scan '/opt' directory ... ..."
    JAVA_PATH_DIR=$(find /opt -name java 2>&1)
    for i in $JAVA_PATH_DIR
    do
        if [[ $i =~ $reg ]]; then
            echo "##### JDK_PATH : $i"
            echo "##### The JDK_PATH is OK!"
            JAVA_PATH=$i
        fi
    done
fi

if [ "$JAVA_PATH" = "" ]; then
    echo "##### '/opt' directory Not found the JDK!"
    echo "##### Start scan '/usr' directory ... ..."
    JAVA_PATH_DIR=$(find /usr -name java 2>&1)
    for i in $JAVA_PATH_DIR
    do
        if [[ $i =~ $reg ]]; then
            echo "##### JDK_PATH : $i"
            echo "##### The JDK_PATH is OK!"
            JAVA_PATH=$i
        fi
    done
fi
if [ "$JAVA_PATH" = "" ]; then
    echo "##### '/usr' directory Not found the JDK!"
    msg="The JDK Not found, Please configure the JDK!"
    echo "##### $msg"
    failed "$msg"
    exit
fi

# ### 获取jar文件,若多个jar文件,默认选择使用最新文件
echo "##### Get running jar file, if more than jar file, the default option is to use the latest file."
JarFileName=$(ls -lt $PRGDIR/*.jar 2>&1|awk '/^-/ {print $NF}'|sed -n '1p')
if [ -z "$JarFileName" ]; then
    msg="The jar file is not exist! Please confirm before implementation."
    echo "##### $msg"
    failed "$msg"
    exit
fi

echo "##### Currently Running JarFileName: $JarFileName"

# ### 设置JVM参数
CATALINA_OPTS="-Xms8192m -Xmx8192m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -XX:+UseConcMarkSweepGC"
RUN_COM="nohup $JAVA_PATH -jar $CATALINA_OPTS $JarFileName 2>&1"
echo "##### Run the command : $RUN_COM"
#nohup $JAVA_PATH -jar $CATALINA_OPTS $JarFileName > nohup.out 2>&1 &
setsid $JAVA_PATH -jar $CATALINA_OPTS $JarFileName > nohup.out 2>&1 &
# ### 将jar包启动对应的pid写入文件中，为停止时提供pidi
# echo $! > run.pid

if [ $? -eq 0 ]; then
    success
    if [  "$P1" == "-f" ]; then
        echo "#################### tail -f nohup.out #########################"
        tail -f nohup.out
    fi
else
    failed "$msg"
fi